<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        li {
            list-style: none;
        }
    </style>
</head>

<body>
    <div class="list">
        <ul>

        </ul>
    </div>
</body>

</html>
<script src="data.json"></script>
<script>
    function objString(obj) {
        // 定义一个新对象
        var str = '';
        // 遍历数组，转为字符串
        for (var key in obj) {
            str += `${key}=${obj[key]}&`
        }
        return str.slice(0, str.length - 1)
    }
    // Ajax封装
    function ajax(method, url, params, cb) {
        var xhr = new XMLHttpRequest();
        xhr.open('get', url + "?" + objString(params))
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                cb(JSON.parse(xhr.responseText))
            }
        }
        xhr.send()
    }
    ajax('get', `./data.json`, function (data) {
        var str = data.emotions.map((item, index) => {
            return `<li> ${item.title} </li> `
        }).join('')

        document.querySelector('.list').innerHTML = str;
    })



    // var xhr=new XMLHttpRequest()
    // xhr.open('get','./data.json')
    // xhr.onreadystatechange=function(){
    //     if(xhr.readyState==4){
    //         console.log('请求到了数据',JSON.parse(xhr.responseText));
    //     }
    // }
    // xhr.send()
</script>